package dao;

import data.Manager;
import data.Teacher;
import service.DbConnect;
import service.Tool;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class ManagerDao {
    private static DbConnect<Manager> c=new DbConnect();
    public static boolean edit(String pwd){
        try {
            String sql="update teacher set pwd=? where id=1";
            PreparedStatement preparedStatement = c.preparedStatement(sql);
            preparedStatement.setString(1,pwd);
            c.que(preparedStatement);
            return true;
        } catch (SQLException e) {
            System.out.println("错误："+e.getMessage());
            return false;
        }
    }

    public static String login(Manager m) throws Exception {
        Manager manager = c.selectOne("select * from manager where user=" + m.getUser() + " and pwd=" + m.getPwd(), m);
        if(manager==null)
            return null;
        else{
            String session=Tool.session();
            insertToken(manager.getId(),session);
            return session;
        }
    }

    public static void insertToken(int id,String session) throws Exception {
        String sql="update manager set session="+ session+" where id="+id;
        c.execute(sql);
    }
}
